/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */
package info.peigen.hotpot.component.doc.core.resolver;

import info.peigen.hotpot.common.core.result.ListResult;
import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.common.core.result.ResultBase;
import info.peigen.hotpot.common.core.result.SingleResult;
import info.peigen.hotpot.component.doc.core.annotation.Api;
import info.peigen.hotpot.component.doc.core.domain.HotpotApi;
import org.beetl.sql.core.page.PageRequest;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * <b>(HotpotApiResolver)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024/3/29
 */
public class HotpotApiResolver {
    public static HotpotApi resolver(Api anno) {
        HotpotApi api = HotpotApi.builder()
                .name(anno.name())
                .code(anno.code())
                .type(anno.type())
                .description(anno.description())
                .build();
        Class<?> orderClass = anno.order();
        HotpotDocAttributeResolver.resolverInAttribute(api, orderClass);
        Class<?> resultClass = anno.result();
        Type     type        = resultClass.getGenericSuperclass();

        if (type instanceof ParameterizedType p) {
            Class<?> infoClass = (Class<?>) p.getActualTypeArguments()[0];
            HotpotDocAttributeResolver.resolverOutAttribute(api, infoClass);
        } else if (type instanceof SingleResult) {
            HotpotDocAttributeResolver.resolverOutAttribute(api, SingleResult.class);
        } else if (type instanceof ListResult<?>) {
            HotpotDocAttributeResolver.resolverOutAttribute(api, ListResult.class);
        } else if (type instanceof PageRequest<?>) {
            HotpotDocAttributeResolver.resolverOutAttribute(api, PageResult.class);
        } else {
            HotpotDocAttributeResolver.resolverOutAttribute(api, ResultBase.class);
        }

        return api;
    }

}
